<?php


namespace app\api\controller;


use Firebase\JWT\JWT;
use think\Controller;
use think\response\Json;

class BaseController extends Controller
{
// Session('user', $user_id);

    protected function initialize()
    {
        parent::initialize();
        $header = $this->request
 }

    /**
     * Notice:获取jwt的token信息
     * Date  :2019/3/12
     */
    public static function getJwtToken($uid)
    {
        $key = config('jwt.key');
        $payload = [
            'lat' => config('jwt.lat'),
            'nbf' => config('jwt.nbf'),
            'exp' => config('jwt.exp'),
            'uid' => $uid,
        ];

        $jwtToken = JWT::encode($payload, $key);

        return $jwtToken;

    }

    /**
     * 返回封装后的 API 数据到客户端
     * @param int|null $status 状态码
     * @param string $message
     * @param array $data
     * @return array|Json
     */
    protected function renderJson(int $status = null, string $message = '', array $data = [])
    {
        return json(compact('status', 'message', 'data'));
    }

    /**
     * 返回操作成功json
     * @param array|string $data
     * @param string $message
     * @return array
     */
    protected function renderSuccess($data = [], string $message = 'success')
    {
        if (is_string($data)) {
            $message = $data;
            $data = [];
        }
        return $this->renderJson(config('status.success'), $message, $data);
    }

    /**
     * 返回操作失败json
     * @param string $message
     * @param array $data
     * @return array
     */
    protected function renderError(string $message = 'error', array $data = [])
    {
        return $this->renderJson(config('status.error'), $message, $data);
    }
}